package com.manger.format;

import com.manger.common.office.sax.ExcelFactory;
import com.manger.model.Project;
import com.manger.model.SplitPayHappy;
import com.manger.utils.*;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@ClassName SplitPayCaseFilesFormat
 *@Description  TODO
 *@Author @LiuHongJun
 *@Date 2025/9/1 16:00
 *@Version 1.0
 */
public class SplitPayCaseFilesFormat implements CaseFilesFormat{

    @Override
    public void format(String extractDir, String targetDir, Project project) throws Exception {
        System.out.println("++++++++++++++++++++++分期乐案件数据整理+++++++++++++++++++++++++++++");
        List<String> ignoreFileList = project.getIgnoreFilesList();
        List<String> ignoreDirList = project.getIgnoreDirList();
        String excelFilePath = extractDir + "/" + SystemConstant.ENTRUSTED_INFO;
        System.out.println("读取委托信息表：" + excelFilePath);
        Map<String,List<?>> map = ExcelFactory.getExcelFactory().getParseExecutor().parse(new File(excelFilePath), SplitPayHappy.class).get();
        if(map.keySet().size() <= 0){
            System.out.println("没有解析到委托信息表数据！");
            return;
        }
        List<SplitPayHappy> splitPayHappyList = (List<SplitPayHappy>) map.get(map.keySet().iterator().next());
        if(splitPayHappyList == null){
            System.out.println("没有解析到委托信息表数据！");
            return;
        }
        File pDir= new File(extractDir + "/" + project.getDebtName());
        if(!pDir.exists()){
            System.out.println("沒有找到文件信息！");
            return;
        }
        Map<String,SplitPayHappy> splitPayHappyMap = new HashMap<>();
        Map<String,String> idCardMap = new HashMap<>();
        for(SplitPayHappy splitPayHappy: splitPayHappyList){
            splitPayHappyMap.put(splitPayHappy.getUid(),splitPayHappy);
        }
        File[] dirFiles = pDir.listFiles();
        for(File dir : dirFiles){
            File[] dirFiles2 = dir.listFiles();
            for(File tempDir: dirFiles2){
                File[] files = tempDir.listFiles();
                for(File file : files){
                    String dirName = file.getName();
                    File[] dfiles = file.listFiles();
                    SplitPayHappy splitPayHappy = splitPayHappyMap.get(dirName);
                    if(splitPayHappy == null){
                        continue;
                    }
                    String tempTargetDir = targetDir + "/" + project.getDebtName() + "/" + SystemConstant.CASE_FILE_DIR_NAME
                            + "/" + splitPayHappy.getUserName() + "-" + splitPayHappy.getIdCard() ;
                    for(File e : dfiles){
                        if(e.getName().contains("法诉明细") || e.getName().endsWith(".zip")){
                            continue;
                        }
                        CopyFiles.copyFileToDir(e.getCanonicalPath(),tempTargetDir + "/" + LoanFileEnum.EVIDENCE_MATERIALS.getName());
                    }
                    System.out.println("整理案件成功【"+splitPayHappy.getUserName()+"-"+splitPayHappy.getIdCard()+"】");
                    idCardMap.put(splitPayHappy.getIdCard(),tempTargetDir);
                }
            }
        }
        System.out.println("整理公共材料");
        for(String idCard: idCardMap.keySet()){
            CopyFiles.copyBaseFiles(idCardMap.get(idCard),project);
        }
        CopyFiles.copyOtherMaterialFileToDir(extractDir,idCardMap);
    }

    public static void main(String[] args) throws Exception {
        ChinaAreaTools.init();
        ProjectUtils.init();
        SplitPayCaseFilesFormat splitPayCaseFilesFormat = new SplitPayCaseFilesFormat();
        String target = "C:\\Users\\liuhj\\Desktop\\案件整理结果";
        String extractDir = "C:\\Users\\liuhj\\Desktop\\分期乐";
        Project project = new Project();
        project.setDebtName(DetNameEnum.SPLIT_PAY.getDesc());
        splitPayCaseFilesFormat.format(extractDir,target,project);
    }
}
